#include "bodypart_soft.h"

// BodypartSoft::BodypartSoft(btDynamicsWorld* ownerWorld, void* owner, btCollisionShape* nshape, float weight, btTransform& transform, const Color& clr, bodypartShape sh)
BodypartSoft::BodypartSoft(bodypartShape sh, const BeColor& clr, btDynamicsWorld* ownerWorld)
 : Bodypart( sh, SOFT, clr, ownerWorld )
{
}

void BodypartSoft::generalSetup( btSoftBodyWorldInfo* btsoftbodyInfo, btVector3& localInertia, float weight, btTransform& transform, void* owner, float lin_damping, float ang_damping )
{
/*	if (weight != 0.f) // weight of non zero = dynamic
		shape->calculateLocalInertia( weight, localInertia );

	myMotionState = new btDefaultMotionState( transform );
	
	btRigidBody::btRigidBodyConstructionInfo rbInfo( weight, myMotionState, shape, localInertia);
	body = new btRigidBody(rbInfo);

	body->setUserPointer(owner);
	body->setDamping(lin_damping, ang_damping);
	body->setDeactivationTime(0.0001);
	body->setSleepingThresholds(0.36, 0.05);

	m_ownerWorld->addRigidBody(body);*/
}

void BodypartSoft::copyPosition()
{
	drawbuffer.clear();
	btVector3 da;
	btVector3 db;
	btVector3 dc;
	for( int i=0;i < body->m_faces.size();++i)
	{
		const btSoftBody::Face& f = body->m_faces[i];
		da = f.m_n[0]->m_x;
		db = f.m_n[1]->m_x;
		dc = f.m_n[2]->m_x;
		const btVector3	n=btCross(db-da,dc-da).normalized();

		drawbuffer.push_back( btVector3( n.getX(),n.getY(),n.getZ() ) );
		drawbuffer.push_back( btVector3( da.getX(),da.getY(),da.getZ() ) );
		drawbuffer.push_back( btVector3( db.getX(),db.getY(),db.getZ() ) );
		drawbuffer.push_back( btVector3( dc.getX(),dc.getY(),dc.getZ() ) );
	}
}

BodypartSoft::~BodypartSoft()
{
// 	// Remove all bodies and shapes
// 	m_ownerWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(body->getBroadphaseHandle(),m_ownerWorld->getDispatcher());
// 	m_ownerWorld->removeRigidBody(body);
// 	delete myMotionState;
// 	delete body->getMotionState();
	delete body;
}


